package cn.ivicar.oss.controller;

import cn.com.carsmart.exception.CarsmartException;
import cn.com.carsmart.ws.ibatis.model.BaseExample;
import cn.com.carsmart.ws.model.ActivateStat;
import cn.com.carsmart.ws.model.TotalStatistic;
import cn.com.carsmart.ws.model.VehicleFirstPosition;
import cn.com.carsmart.ws.model.VehicleState;
import cn.com.carsmart.ws.service.ActivateStatManager;
import cn.com.carsmart.ws.service.TotalStatisticManager;
import cn.com.carsmart.ws.service.VehicleFirstPositionManager;
import cn.com.carsmart.ws.service.VehicleStateManager;
import com.google.inject.Inject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

/**
 * @Author: Administrator
 * @Description:数据统计控制层
 * @Date: Created in 2017/5/16
 */
public class StatisticsController {

    private static final String IOV_INTERACTION = "iov:interaction";
    private static final String IOV_STAUTUS = "iov:status";

    @Inject
    private VehicleStateManager vehicleStateManager;
    @Inject
    private ActivateStatManager activateStatManager;
    @Inject
    private TotalStatisticManager totalStatisticManager;
    @Inject
    private VehicleFirstPositionManager vehicleFirstPositionManager;

    /*
    设备总数统计
     */
    public JSONObject getTotalStat() throws CarsmartException {

        //已激活设备总数
        int activateStat = 0;
        int offLineCount = 0;//离线总数
        int onLineStat = 0;//在线总数

        BaseExample example = this.totalStatisticManager.getExample();
        example.setOrderByClause(" id desc ");
        example.setLimitStart(0);
        example.setLimitCount(1);
        List<TotalStatistic> count = this.totalStatisticManager.listByBaseExample(example);
        if(count != null && count.size() >0){
            TotalStatistic totalStatistic = count.get(0);
            activateStat = totalStatistic.getInteractionCount();
            offLineCount = totalStatistic.getOfflineCount();
            onLineStat = totalStatistic.getOnlineCount();
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("activateStat", activateStat);
        jsonObject.put("onLineCount", onLineStat);
        jsonObject.put("offLineCount", offLineCount);

        return jsonObject;
    }

    //获取设备经纬度信息
    public JSONArray getHotMapData(Boolean tag) throws CarsmartException {
        BaseExample example = this.vehicleStateManager.getExample();
        example.or().andPropertyEqualTo(tag,"online");
        List<VehicleState> vehicleStates = this.vehicleStateManager.listByBaseExample(example);
        JSONArray gpsList = new JSONArray();
        String[] gps = new String[2];
        if (vehicleStates != null && vehicleStates.size() > 0) {
            for (VehicleState vs : vehicleStates) {
                JSONObject jsonObject = new JSONObject();
                gps[0] = String.valueOf(vs.getLongitude());
                gps[1] = String.valueOf(vs.getLatitude());
                jsonObject.put("lnglat", gps);
                gpsList.add(jsonObject);
            }
        }

        return gpsList;
    }

    /*
    获取设备首次激活经纬度信息
     */
    public JSONArray getFirstMapData() throws CarsmartException {
        List<VehicleFirstPosition> list = this.vehicleFirstPositionManager.list();
        JSONArray gpsList = new JSONArray();
        String[] gps = new String[2];
        if (list != null && list.size() > 0) {
            for (VehicleFirstPosition vs : list) {
                JSONObject jsonObject = new JSONObject();
                gps[0] = String.valueOf(vs.getLongitude());//经度
                gps[1] = String.valueOf(vs.getLatitude());//纬度
                jsonObject.put("lnglat", gps);
                gpsList.add(jsonObject);
            }
        }

        return gpsList;
    }

    /*
    新增数量统计
     */
    public JSONArray getActivateStat() throws CarsmartException {

        List<ActivateStat> list = this.activateStatManager.list();
        JSONArray jsonArray = new JSONArray();
        if (list != null && list.size() > 0) {

            for (ActivateStat as : list) {
                JSONObject activate = new JSONObject();
                activate.put("addCount", as.getActivateCount());//当天新增数量
                activate.put("totalCount", as.getTotalCount());//截止目前总数量
                activate.put("time", as.getStatTime().getTime());//统计时间
                jsonArray.add(activate);
            }
        }

        return jsonArray;
    }
}
